package com.health.controller;

import com.health.dto.ApiResponse;
import com.health.dto.ArticleRequest;
import com.health.dto.ArticlePageRequest;
import com.health.dto.PageResponse;
import com.health.model.Article;
import com.health.model.ArticleStatus;
import com.health.model.User;
import com.health.security.AdminRequired;
import com.health.service.ArticleService;
import com.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/articles")
public class ArticleController {
    
    @Autowired
    private ArticleService articleService;
    
    @Autowired
    private UserService userService;
    
    @AdminRequired
    @GetMapping("/admin/list")
    public ResponseEntity<ApiResponse<?>> getArticleList(ArticlePageRequest request) {
        try {
            PageResponse<Article> response = articleService.getArticleList(request);
            return ResponseEntity.ok(ApiResponse.success(response));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    @AdminRequired
    @PostMapping("/admin/create")
    public ResponseEntity<ApiResponse<?>> createArticle(
            @RequestBody ArticleRequest request,
            @RequestAttribute Long userId) {
        try {
            User author = userService.getUserById(userId);
            Article article = articleService.createArticle(request, author);
            return ResponseEntity.ok(ApiResponse.success(article));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    @AdminRequired
    @PutMapping("/admin/{id}")
    public ResponseEntity<ApiResponse<?>> updateArticle(
            @PathVariable Long id,
            @RequestBody ArticleRequest request) {
        try {
            Article article = articleService.updateArticle(id, request);
            return ResponseEntity.ok(ApiResponse.success(article));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    @AdminRequired
    @PutMapping("/admin/{id}/status")
    public ResponseEntity<ApiResponse<?>> updateArticleStatus(
            @PathVariable Long id,
            @RequestParam ArticleStatus status) {
        try {
            articleService.updateArticleStatus(id, status);
            return ResponseEntity.ok(ApiResponse.success(null));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
    
    @AdminRequired
    @DeleteMapping("/admin/{id}")
    public ResponseEntity<ApiResponse<?>> deleteArticle(@PathVariable Long id) {
        try {
            articleService.deleteArticle(id);
            return ResponseEntity.ok(ApiResponse.success(null));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
} 